package com.wyl.server.search

import android.view.View
import android.widget.EditText
import androidx.databinding.ObservableArrayList
import androidx.recyclerview.widget.LinearLayoutManager
import com.wuhenzhizao.titlebar.widget.CommonTitleBar.ACTION_LEFT_BUTTON
import com.wuhenzhizao.titlebar.widget.CommonTitleBar.ACTION_RIGHT_BUTTON
import com.wyl.libbase.base.NormalActivity
import com.wyl.libbase.binding.recyclerview.RecyclerViewDivider
import com.wyl.libbase.extend.autoWired
import com.wyl.libbase.extend.toast
import com.wyl.server.R
import io.ditclear.bindingadapter.ItemClickPresenter
import io.ditclear.bindingadapter.SingleTypeAdapter
import kotlinx.android.synthetic.main.search_list_activity.*
import kotlinx.android.synthetic.main.search_title_center.view.*

class SearchListActivity : NormalActivity(), ItemClickPresenter<Map<String, String>> {

    private val key by lazy { autoWired("key", "") }
    val dataSource = ObservableArrayList<Map<String, String>>()

    override fun getLayoutId(): Int = R.layout.search_list_activity

    override fun initView() {
        titlebar.apply {
            val text = centerCustomView.editText
            setListener { _, action, _ ->
                when (action) {
                    ACTION_LEFT_BUTTON -> onBackPressed()
                    ACTION_RIGHT_BUTTON -> {
                        if (text.text.isEmpty()) {
                            toast(R.string.search_empty)
                        } else {
                            // todo:搜索内容
                        }
                    }
                    else -> {
                    }
                }
            }
        }

        recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
            addItemDecoration(RecyclerViewDivider(context, RecyclerViewDivider.VERTICAL))
        }

        refreshLayout.apply {
            setEnableLoadmore(false)
            setOnRefreshListener(loadListener)
        }

    }

    private val mAdapter by lazy {
        SingleTypeAdapter(this, R.layout.service_item2, dataSource)
            .apply {
                itemPresenter = object : ItemClickPresenter<Any> {
                    override fun onItemClick(v: View, item: Any) {

                    }
                }
            }
    }

    override fun onItemClick(v: View, item: Map<String, String>) {

    }

    override fun loadData() {
        refreshLayout.startRefresh()
//        OkGo.post<BaseResponse<List<Map<String, String>>>>(HOME_SEARCH)
//            .params("name", key)
//            .converter(object : JsonConvert<BaseResponse<List<Map<String, String>>>>() {})
//            .adapt(ObservableBody<BaseResponse<List<Map<String, String>>>>())
//            .doFinally {
//                refreshLayout.finishRefreshing()
//                refreshLayout.setEnableRefresh(false)
//            }
//            .subscribe(
//                {
//                    dataSource.clear()
//                    if (it.code == 1) {
//                        empty.visibility = View.VISIBLE
//                    } else {
//                        dataSource.addAll(it.data)
//                    }
//                }, {
//                    onError(it)
//                    it.printStackTrace()
//                }
//            ).toDisposables(disposables)
    }

}
